﻿using System;
using System.IO;
using IterationLife.Datas;
using IterationLife.Extensions.Qiniu;
using IterationLife.FootballProfiles.Models;
using IterationLife.FootballProfiles.Repositories;
using IterationLife.FootballTeaching.Repositories;
using IterationLife.Mobile.AppServices.Dtos.UserProfiles;
using IterationLife.System.Repositories;
using IterationLife.System.Services;
using IterationLife.UserProfiles.Models;
using IterationLife.UserProfiles.Repositories;
using Util.Exceptions;

namespace IterationLife.Mobile.AppServices.Services.UserProfiles.Implements
{
    public class UserProfileService : IUserProfileService
    {
        private readonly IApplicationUnitOfWork _unitOfWork;
        private readonly IAttachmentManager _attachmentManager;
        private readonly IUserRepository _userRepository;
        private readonly IUserProfileRepository _userProfileRepository;
        private readonly IUserBodyInfoRepository _userBodyInfoRepository;
        private readonly IUserFootballProfileRepository _userFootballProfileRepository;
        private readonly IPlayerPositionRepository _playerPositionRepository;
        private readonly IDistrictRepository _districtRepository;

        /// <summary>
        /// 用户资料服务
        /// </summary>
        public UserProfileService(IApplicationUnitOfWork unitOfWork,
            IAttachmentManager attachmentManager,
            IUserRepository userRepository,
            IUserProfileRepository userProfileRepository,
            IUserBodyInfoRepository userBodyInfoRepository,
            IUserFootballProfileRepository userFootballProfileRepository,
            IPlayerPositionRepository playerPositionRepository,
            IDistrictRepository districtRepository)
        {
            _unitOfWork = unitOfWork;
            _attachmentManager = attachmentManager;
            _userRepository = userRepository;
            _userProfileRepository = userProfileRepository;
            _userBodyInfoRepository = userBodyInfoRepository;
            _userFootballProfileRepository = userFootballProfileRepository;
            _playerPositionRepository = playerPositionRepository;
            _districtRepository = districtRepository;
        }

        public void EditUserProfiles(Guid userId, EditUserProfileInput input)
        {
            var user = _userRepository.Single(d => d.Id == userId);
            if (user == null)
                throw new Warning("用户不存在");
            var userProfile = GetOrCreateUserProfile(userId);
            var userBodyInfo = GetOrCreateUserBodyInfo(userProfile.Id);
            var userFootballProfile = GetOrCreateUserFootballProfile(userProfile.Id);

            //输入校验，todo
            userProfile.NickName = input.NickName;
            userProfile.Avatar = input.Avatar;
            userProfile.Birthday = input.Birthday;
            userProfile.Gender = input.Gender;
            if (!string.IsNullOrWhiteSpace(input.LocationCode))
            {
                var district = _districtRepository.Single(d => d.Code == input.LocationCode);
                userProfile.LocationCode = input.LocationCode;
                userProfile.Location = district.MergerName;              
            }
            userBodyInfo.Height = input.Height;
            userBodyInfo.Weight = input.Weight;
            userFootballProfile.FootballNumber = input.FootballNumber;
            if (!string.IsNullOrWhiteSpace(input.PositionCode))
            {
                var playerPosition = _playerPositionRepository.Single(d => d.Code == input.PositionCode);
                userFootballProfile.Position = playerPosition.Name;
                userFootballProfile.PositionCode = input.PositionCode;
            }
            _unitOfWork.Commit();
        }

        public UserProfileOutput GetUserProfiles(Guid userId)
        {
            var user = _userRepository.Single(d => d.Id == userId);
            if (user == null)
                throw new Warning("用户不存在");
            var userProfile = GetOrCreateUserProfile(userId);
            var userBodyInfo = GetOrCreateUserBodyInfo(userProfile.Id);
            var userFootballProfile = GetOrCreateUserFootballProfile(userProfile.Id);
            _unitOfWork.Commit();
            return new UserProfileOutput
            {
                NickName = userProfile.NickName,
                Avatar = userProfile.Avatar,
                Birthday = userProfile.Birthday,
                Gender = userProfile.Gender,
                Location =userProfile.Location,
                LocationCode = userProfile.LocationCode,
                Height = userBodyInfo.Height,
                Weight = userBodyInfo.Weight,
                FootballNumber = userFootballProfile.FootballNumber,
                PositionName = userFootballProfile.Position,
                PositionCode = userFootballProfile.PositionCode,
            };

        }

        #region 私有方法

        private UserProfile GetOrCreateUserProfile(Guid userId)
        {
            var userProfile = _userProfileRepository.Single(d => d.UserId == userId);
            if (userProfile == null)
            {
                userProfile = new UserProfile(Guid.NewGuid())
                {
                    UserId = userId,
                    CreationTime = DateTime.Now,
                };
                _userProfileRepository.Add(userProfile);
            }
            return userProfile;
        }

        private UserBodyInfo GetOrCreateUserBodyInfo(Guid userProfileId)
        {
            var userBodyInfo = _userBodyInfoRepository.Single(d => d.UserProfileId == userProfileId);
            if (userBodyInfo == null)
            {
                userBodyInfo = new UserBodyInfo(Guid.NewGuid())
                {
                    UserProfileId = userProfileId,
                    CreationTime = DateTime.Now,
                };
                _userBodyInfoRepository.Add(userBodyInfo);
            }
            return userBodyInfo;
        }

        private UserFootballProfile GetOrCreateUserFootballProfile(Guid userProfileId)
        {

            var userFootballProfile = _userFootballProfileRepository.Single(d => d.UserProfileId == userProfileId);
            if (userFootballProfile == null)
            {
                userFootballProfile = new UserFootballProfile(Guid.NewGuid())
                {
                    UserProfileId = userProfileId,
                    CreationTime = DateTime.Now,
                };
                _userFootballProfileRepository.Add(userFootballProfile);
            }
            return userFootballProfile;
        }

        #endregion

        public string UploadAvatar(Stream inputStream, int size, string fileName)
        {
            var attchment = _attachmentManager.Upload(inputStream, size, fileName);
            _unitOfWork.Commit();
            var avatarUrl = QiNiu.DomainName + attchment.Address;
            return avatarUrl;
        }


    }
}
